package com.itheima.reggie.web.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.dto.SetmealDto;
import com.itheima.reggie.service.SetmealService;
import com.itheima.reggie.web.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.itheima.reggie.utils.RedisConstant.SETMEAL_CACHE;

/**
 * 套餐控制层
 *
 * @Name : SetmealController
 * @Author : WuQiang
 * @Date : 2022-08-19 9:23
 * @Description : 套餐控制层
 */
@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    /**
     * 新增套餐
     *
     * @param setmealDto
     * @return
     */
    @PostMapping
    @CacheEvict(value = SETMEAL_CACHE,allEntries = true)
    public R save(@RequestBody SetmealDto setmealDto) {
        Boolean result = setmealService.saveWithDish(setmealDto);
        if (result) {
            return R.success("新增套餐成功");
        }
        return R.success("新增套餐失败");
    }

    /**
     * 分页查询套餐 根据套餐名称分页查询
     *
     * @param pageNum  当前页
     * @param pageSize 页面大小
     * @param name     套餐名称
     * @return
     */
    @GetMapping("/page")
    public R page(
            @RequestParam(name = "page", required = false, defaultValue = "1") Long pageNum,
            @RequestParam(name = "pageSize", required = false, defaultValue = "10") Long pageSize,
            @RequestParam(name = "name", required = false, defaultValue = "") String name) {
        Page<SetmealDto> dtoPage = setmealService.page(pageNum, pageSize, name);
        return R.success("查询成功", dtoPage);
    }

    /**
     * 删除套餐
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @CacheEvict(value = SETMEAL_CACHE,allEntries = true)
    public R delete(Long[] ids) {
        Boolean result = setmealService.delByIds(ids);
        if (result) {
            return R.success("删除套餐成功");
        }
        return R.success("删除套餐失败");
    }

    /**
     * 修改套餐状态
     *
     * @param status 0:停用 1:启用
     * @param ids    套餐id
     * @return
     */
    @PostMapping("/status/{status}")
    @CacheEvict(value = SETMEAL_CACHE,allEntries = true)
    public R updateStatus(@PathVariable Integer status,
                          @RequestParam(value = "ids") Long[] ids) {
        log.info("修改套餐状态，状态：{},id：{}", status, ids);
        //参数校验
        if (ObjectUtils.isEmpty(status) || ObjectUtils.isEmpty(ids)) {
            return R.error("参数非法");
        }
        Boolean result = setmealService.updateStatus(status, ids);
        if (result) {
            return R.success("修改套餐状态成功");
        }
        return R.success("修改套餐状态失败");
    }

    /**
     * 根据套餐id查询回显套餐相关信息
     *
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public R findById(@PathVariable Long id) {
        //参数校验
        if (ObjectUtils.isEmpty(id)) {
            return R.error("参数非法");
        }
        SetmealDto dto = setmealService.fingById(id);
        return R.success("根据id查询成功", dto);
    }

    /**
     * 修改套餐信息
     *
     * @param setmealDto
     * @return
     */
    @PutMapping
    @CacheEvict(value = SETMEAL_CACHE,allEntries = true)
    public R update(@RequestBody SetmealDto setmealDto) {
        // TODO: 2022/8/19 参数校验
        Boolean result = setmealService.updateWithSetmealDish(setmealDto);
        if (result) {
            return R.success("修改套餐成功");
        }
        return R.success("修改套餐失败");
    }

    /**
     * 根据套餐id跟状态查询套餐
     *
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    @Cacheable(value = SETMEAL_CACHE,key = "#setmeal.categoryId+'_'+#setmeal.status")
    public R getList(Setmeal setmeal) {
        Long categoryId = setmeal.getCategoryId();
        Integer status = setmeal.getStatus();
        List<Setmeal> setmealList = setmealService.list(Wrappers.lambdaQuery(Setmeal.class)
                .eq(categoryId != null, Setmeal::getCategoryId, categoryId)
                .eq(status != null, Setmeal::getStatus, status));
        return R.success("查询套餐成功", setmealList);
    }


}
